import { Component } from '@angular/core';
import { IonicPage, NavController, ViewController, NavParams } from 'ionic-angular';
import { HttpProvider } from '../../providers/http/http';
import { StorageProvider } from '../../providers/storage/storage';
import { DialogProvider } from '../../providers/dialog/dialog';
import { UserInfoProvider } from '../../providers/user-info/user-info';
import { Network } from '@ionic-native/network';
import { user_info } from '../login/login';


/**
 * Generated class for the MyCouponsPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-my-coupons',
  templateUrl: 'my-coupons.html',
})
export class MyCouponsPage {

  member_id:any;
  //用户信息
  userInfo: user_info = new user_info();

  public Coupons = []

  constructor(
		public navCtrl: NavController,
		public viewCtrl: ViewController,
		public navParams: NavParams,
		public http: HttpProvider,
		public storage: StorageProvider,
		private network: Network,
    public dialog: DialogProvider,
    public user: UserInfoProvider){ }
    
    //有网状态=1 断网=0
	Internet = 1;

  ionViewDidLoad() {
    console.log('ionViewDidLoad MyCouponsPage');    
    // this.storage.getStorage("login_info").then(res => {
		// 	console.log(res);
		// 	if (res != undefined) {
		// 		this.member_id = res['member_id']
		// 		this.getCoupons();
		// 	}
		// });
  }

  ionViewDidEnter() {
    if (this.network.type !== 'none' && this.network.type !== '2g') {
			this.Internet = 1;
      this.userInfo = this.user.getuserInfo();
      this.member_id = this.userInfo.member_id;
      this.getCoupons();
  	}
  }

  getCoupons(){
    var timestamp = Math.ceil(new Date().getTime()/1000);
    this.http.postJson('','member/memberco/index?page=1',{
      "where":['and',["=","member_id",this.member_id],["=","is_use",'0'],[">=","overdue_time",timestamp]],
      "limit":15,
      "joinwith":["ma_coupon"]
    },'').then(res => {
      console.log(res)
      if(res['status']==200){
        this.getMessage(res['data']);
      }
    });
  }
   

  getMessage(Coupons){
   this.Coupons.splice(0);
   console.log(Coupons)
   
     for(let coupons of Coupons){
       if(coupons.ma_coupon){
          var remarks = coupons.ma_coupon.remarks;
         
          if(coupons.surplus_num >1){
              var a={
              // expiry_date:(parseInt(coupons.created_at) + parseInt(coupons.ma_coupon.expiry_date)*24*60*60)*1000,//券有效期
              expiry_date:parseInt(coupons.overdue_time)*1000,//券有效期
              denomination:coupons.ma_coupon.denomination,//代金券面额
              coupon_name:coupons.ma_coupon.coupon_name + "x"+coupons.surplus_num,//卷名称
              remarks:coupons.ma_coupon.remarks,//备注
             };
          }else{ 
               a={
              // expiry_date:(parseInt(coupons.created_at) + parseInt(coupons.ma_coupon.expiry_date)*24*60*60)*1000,//券有效期
              expiry_date:parseInt(coupons.overdue_time)*1000,//券有效期
              denomination:coupons.ma_coupon.denomination,//代金券面额
              coupon_name:coupons.ma_coupon.coupon_name,//卷名称
              remarks:coupons.ma_coupon.remarks,//备注
             };
          }
         
         if(new Date().getTime() < a.expiry_date){
          this.Coupons.push(a);
         }
         }
     }
 }
  	
  	CouponsDetail(remarks) {
		this.dialog.showAlert('',remarks);
	}
}

